/*
 * settings.cpp
 *
 *  Created on: 2024年6月21日
 *      Author: liangjp
 */
#include "entry/EasyUIContext.h"
#include "manager/LanguageManager.h"
#include "system/storage.h"
#include "utils/TimeHelper.h"
#include "can/context.h"
#include "menu_set.h"
#include "base/log.hpp"
#include <unordered_set>

namespace menu_set{

typedef enum {
	RESET_E,	//重置里程A或B，对应canID=0x1FFF0086
	ODO_E,		//里程同步信息，对应canID=0x1FFF0084
	TIME_SET_E,	//时间设置，对应canID=0x1FFF0087
}CAN_DATA_SET;

static can_frame can_send_table[] = {
		{0x1FFF0086, 8, 0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}},
		{0x1FFF0084, 8, 0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}},
		{0x1FFF0087, 8, 0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}},
};

static int s_tank = 0;


static FORMAT_TIME s_format = TWENTY_FOUR;
static theme s_theme = THEME1;

static const char* languaueTextTab[] = {"简体中文", "English(US)", "بالعربية"};



void init(){
	s_tank = storage::get_int("tank", 0);
	s_format = (FORMAT_TIME)storage::get_int("formatTime", TWENTY_FOUR);
	s_theme = (theme)storage::get_int("theme", THEME1);
}


void sendSetTime(int year,int month,int day,int hour, int minute,int sec)
{
	can_send_table[TIME_SET_E].data[0] = year;
	can_send_table[TIME_SET_E].data[1] = month;
	can_send_table[TIME_SET_E].data[2] = day;
	can_send_table[TIME_SET_E].data[3] = hour;
	can_send_table[TIME_SET_E].data[4] = minute;
	can_send_table[TIME_SET_E].data[5] = sec;
	can::canSend(can_send_table[TIME_SET_E].can_id, \
			can_send_table[TIME_SET_E].can_dlc, can_send_table[TIME_SET_E].data);
}

void sendSetODO(int odo)
{
	can_send_table[ODO_E].data[0] = (odo >> 16) & 0xFF;
	can_send_table[ODO_E].data[1] = (odo >> 8) & 0xFF;
	can_send_table[ODO_E].data[2] = odo & 0xFF;
	can::canSend(can_send_table[ODO_E].can_id, \
				can_send_table[ODO_E].can_dlc, can_send_table[ODO_E].data);
}

void sendTankSize()
{
	can_send_table[RESET_E].data[0] = 3;
	can_send_table[RESET_E].data[1] = s_tank;
	can::canSend(can_send_table[RESET_E].can_id, \
				can_send_table[RESET_E].can_dlc, can_send_table[RESET_E].data);
}
void setSaveAll(int type,int mode)
{
	switch(type)
	{
	case 0:
		s_tank = mode;
		storage::put_int("tank", mode);
		sendTankSize();
		break;
	case 2:
		s_theme = (theme)mode;
		storage::put_int("theme", mode);
		break;
	case 3:
		s_format = (FORMAT_TIME)mode;
		storage::put_int("formatTime", mode);
		break;
	default:
		break;
	}
}

theme getTheme(){
	return s_theme;
}

FORMAT_TIME getFormatTime(){
	return s_format;
}

int gettank(){
	return s_tank;
}


LANGUAGETYPE getCurrentCode(){
	const std::string code = LANGUAGEMANAGER->getCurrentCode();
	LANGUAGETYPE type = CH_E;
	if(code == "zh_CN"){
		type = CH_E;
	}else if(code == "en_US"){
		type = US_E;
	}else if(code == "ar_SA"){
		type = AR_E;
	}
	return type;
}

void updateCurrentCode(LANGUAGETYPE type){
	std::string Code;
	switch(type){
	case CH_E:
		Code = "zh_CN";
		break;
	case US_E:
		Code = "en_US";
		break;
	case AR_E:
		Code = "ar_SA";
		break;
	default:
		break;
	}
	EASYUICONTEXT->updateLocalesCode(Code.c_str());
}

int getLanguaueTextTabSize() {
	return ((sizeof(languaueTextTab)/sizeof(languaueTextTab[0])));
}
const char* getLanguaueTextTabIndex(int index) {
	if(index >= getLanguaueTextTabSize()) {
		return "";
	}
	return languaueTextTab[index];
}



}




